//
//  YHBaseAPI.m
//  APIDemo
//
//  Created by 刘欢庆 on 2017/3/26.
//  Copyright © 2017年 刘欢庆. All rights reserved.
//

#import "YHBaseAPI.h"
@interface YHBaseAPI()<HQAOPDelegate>
@end

@implementation YHBaseAPI
- (instancetype)init
{
    self = [super init];
    if(self)
    {
        self.aopDelegate = self;
    }
    return self;
}
- (BOOL)errorDisplay
{
    return YES;
}

- (NSString *)methodString
{
    switch (self.requestMethod)
    {
        case HQRequestMethodGET:
        {
            return @"GET";
        }
            break;
        case HQRequestMethodPOST:
        {
            return @"POST";
        }
            break;
        case HQRequestMethodDELETE:
        {
            return @"DELETE";
        }
            break;
    }
}



- (NSString *)baseURL
{
    return WEB_HOST_V2;
}

- (NSDictionary *)HTTPHeaderFields
{
    NSMutableDictionary *dict = [[super HTTPHeaderFields] mutableCopy];
    [dict setObject:[self userAgent] forKey:@"User-Agent"];
    
    
    NSString *authorization = [self authorization];;
    if(authorization)
    {
        [dict setObject:[NSString stringWithFormat:@"Basic %@",authorization] forKey:@"Authorization"];
    }
    return dict;
}

- (NSString *)authorization
{
//    NSString *authorization = [[[NSString stringWithFormat:@"%@:%@:%@",[HQAccountManager userno],[HQAccountManager accessToken],APP_ID] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
//    return authorization;
    return @"ODk0NTI3NDoxZWtoS0t2bG0xSDhTUjdrQ3JXVU95OmFuZ2xlcg==";
}

- (NSString *)userAgent
{
//    CGFloat scale = [[UIScreen mainScreen] scale];
//    NSString *geohash = [[HQLocationManager sharedInstance] lastGeoHash];
//    NSString *UserAgent = [NSString stringWithFormat:@"%@/%@ (iOS %@; %@; %@; %@; Scale/%0.2f;%@)"
//                           , APP_ID
//                           , APP_VERSION
//                           , [[UIDevice currentDevice] systemVersion]
//                           , [[UIDevice currentDevice] platform]
//                           , [GlobalConfig useLanguage]
//                           , [NSString stringWithFormat:@"%d*%d", (int)([UIScreen mainScreen].bounds.size.width * scale), (int)([UIScreen mainScreen].bounds.size.height * scale)]
//                           , scale
//                           , geohash?geohash:@""];
//    return UserAgent;
    
    return @"angler/3.3.0 (iOS 11.2; x86_64; en; 750*1334; Scale/2.00;9q8yywdq7)";
}


#pragma mark - HQAOPDelegate
///将要完成成功响应
- (void)api:(HQBaseAPI *)api willSuccCompleteResponse:(id)responseObject error:(NSError **)error
{
//    NSError *err = [APIErrorCodeManager rightResponse:responseObject isAlert:[self errorDisplay]];
//    *error = err;
//    HQLogInfo(ERROR_EXPLAIN,self.methodString,self.requestURL,(int)err.code,err.domain,NSStringFromClass([self class]));
}

///将要完成失败响应
- (void)api:(HQBaseAPI *)api willFailCompleteResponse:(id)responseObject error:(NSError **)error
{
//    NSError *err = [APIErrorCodeManager rightResponse:[responseObject toJSONObject] isAlert:[self errorDisplay]];
//    if(err)//如果能返回错误直接使用错误
//    {
//        *error = err;
//    }
//    else
//    {
//        err = *error;
//        [APIErrorCodeManager errorShortHUD:@500 isAlert:[self errorDisplay]];
//    }
//    HQLogInfo(ERROR_EXPLAIN,self.methodString,self.requestURL,(int)err.code,err.domain,NSStringFromClass([self class]));

}

@end
